package com.hxkj.service.wechat.impl;

import com.hxkj.client.service.AppletUserServiceClient;
import com.hxkj.domain.enums.AppletUserExceptionEnum;
import com.hxkj.domain.po.Applet;
import com.hxkj.domain.po.AppletUser;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.service.wechat.AppletUserService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 * <p>
 * Create by chenwei on 2018/6/14 15:24
 */
@Service("appletUserService")
public class AppletUserServiceImpl implements AppletUserService {

    @Resource
    private AppletUserServiceClient appletUserServiceClient;

    @Resource
    private TokenUtils<AuthUser> tokenUtils;

    @Override
    public ResultPageStruct<AppletUser> getlist(PageTo pageTo, AppletUser appletUser) throws Exception {
        AuthUser authUser = tokenUtils.getTokenUserInfo();
        ParameterDataStruct<AppletUser> appletUserParameterDataStruct = new ParameterDataStruct<>();
        // 设置站点
        if (appletUser.getSiteId() == null) {
            throw new CustomException(AppletUserExceptionEnum.SITE_ID_IS_EMPTY);
        }
        // 设置userId
        appletUser.setAgentId(authUser.getUserId());
        appletUserParameterDataStruct.setData(appletUser);
        appletUserParameterDataStruct.setPageTo(pageTo);
        try {
            ResultPageStruct<AppletUser> appletUserResultPageStruct = appletUserServiceClient.getlist(appletUserParameterDataStruct);
            if (appletUserResultPageStruct.getCode() != 200) {
                throw new CustomException(AppletUserExceptionEnum.REQUEST_APPLET_SERVICE_ERROR);
            } else {
                return appletUserResultPageStruct;
            }
        } catch (CustomException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AppletUserExceptionEnum.REQUEST_APPLET_SERVICE_ERROR);
        }
    }

    @Override
    public List<Map<String, Object>> export(PageTo pageTo, AppletUser appletUser) throws Exception {
        AuthUser authUser = tokenUtils.getTokenUserInfo();
        // 设置站点
        if (appletUser.getSiteId() == null) {
            throw new CustomException(AppletUserExceptionEnum.SITE_ID_IS_EMPTY);
        }
        // 设置userId
        appletUser.setAgentId(authUser.getUserId());
        ParameterDataStruct<AppletUser> appletUserParameterDataStruct = new ParameterDataStruct<>();
        appletUserParameterDataStruct.setData(appletUser);
        appletUserParameterDataStruct.setPageTo(pageTo);
        try {
            List<Map<String, Object>> appletUserResultPageStruct = appletUserServiceClient.export(appletUserParameterDataStruct);
            return appletUserResultPageStruct;
        } catch (CustomException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AppletUserExceptionEnum.REQUEST_APPLET_SERVICE_ERROR);
        }
    }

    @Override
    public List<Applet> getlist(AuthUser authUser) throws Exception {
        try {
            // 获取下拉框
            ResultDataStruct<List<Applet>> appletUserResultPageStruct = appletUserServiceClient.getlist(authUser);
            if (appletUserResultPageStruct.getCode() != 200) {
                throw new CustomException(AppletUserExceptionEnum.REQUEST_APPLET_SERVICE_ERROR);
            } else {
                return appletUserResultPageStruct.getData();
            }
        } catch (CustomException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(AppletUserExceptionEnum.REQUEST_APPLET_SERVICE_ERROR);
        }
    }

    public void delete(AppletUser appletUser) throws Exception{
        try{
            if(StringUtils.isEmpty(appletUser.getUserId())){
                throw new CustomException(AppletUserExceptionEnum.AGENT_ID_IS_EMPTY);
            }
            ResultMessageStruct resultMessageStruct  =appletUserServiceClient.delete(appletUser);
            if(resultMessageStruct.getCode() != 200){
                throw new CustomException(AppletUserExceptionEnum.PHONE_DELETE_FAILED);
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
